PIC24F
UART Peripheral Module Library Help


Table Of Contents

1     Library Features. 3

2     Using Library Functions in Your Code. 3

3     Functions. 5

3.1        BusyUART1 , BusyUART2. 5

3.2        CloseUART1 , CloseUART2. 5

3.3        ConfigIntUART1 , ConfigIntUART2. 6

3.4        DataRdyUART1 , DataRdyUART2. 6

3.5        OpenUART1 , OpenUART2. 6

3.6        ReadUART1 , ReadUART2. 7

3.7        WriteUART1 , WriteUART2. 8

3.8        getsUART1 , getsUART2. 8

3.9        putsUART1 , putsUART2. 8

3.10       getcUART1 , getcUART2. 9

3.11       putcUART1 , putcUART2. 9

4     Macros. 9

4.1        EnableIntU1RX , EnableIntU2RX.. 9

4.2        EnableIntU1TX , EnableIntU1TX.. 9

4.3        DisableIntU1RX , DisableIntU1RX.. 9

4.4        DisableIntU1TX , DisableIntU1TX.. 9

4.5        SetPriorityIntU1RX SetPriorityIntU2RX.. 10

4.6        SetPriorityIntU1TX , SetPriorityIntU2TX.. 10

 


1          Library Features

 

This peripheral library module:

 

·         Supports BYTE and WORD transmission and reception.

·         Incorporates multiple BYTE and WORD transmission and reception in a single function call.

·         Provides simple functions to read from and write to the buffers.

·         Provides simple interface macros to enable/disable interrupts.

 

2          Using Library Functions in Your Code

 

Library routine parameters can be constructed using either AND based mask or AND_OR based mask setting. For more information on these masks, see 16-bit Peripheral Libraries.

 

Examples of use for both the methods are below.

 

Example of Use ( AND mask )

 

#include<uart.h>

 

/* Received data is stored in array Buf */

char Buf[80];

char * Receivedddata = Buf;

 

/* This is UART1 transmit ISR */

void __attribute__((__interrupt__)) _U1TXInterrupt(void)

{

IFS0bits.U1TXIF = 0;

}

 

/* This is UART1 receive ISR */

void __attribute__((__interrupt__)) _U1RXInterrupt(void)

{

   IFS0bits.U1RXIF = 0;

   /* Read the receive buffer till atleast one or more character can be

      read */

   while( DataRdyUART1())

   {

   ( *( Receiveddata)++) = ReadUART1();

   }

}

 

int main(void)

{

/* Data to be transmitted using UART communication module */

   char Txdata[] = {'M','i','c','r','o','c','h','i','p',

                    ' ','I','C','D','2','\0'};

/* Holds the value of baud register */

   unsigned int baudvalue;

/* Holds the value of uart config reg */

   unsigned int U1MODEvalue;

/* Holds the information regarding uart

   TX & RX interrupt modes */

   unsigned int U1STAvalue;

/* Turn off UART1module */

   CloseUART1();

/* Configure uart1 receive and transmit interrupt */

   ConfigIntUART1(UART_RX_INT_EN & UART_RX_INT_PR6 &

                  UART_TX_INT_DIS & UART_TX_INT_PR2);

/* Configure UART1 module to transmit 8 bit data with one stopbit.

   Also Enable loopback mode */

   baudvalue = 5;

   U1MODEvalue = UART_EN & UART_IDLE_CON &

   UART_DIS_WAKE & UART_EN_LOOPBACK &

   UART_EN_ABAUD & UART_NO_PAR_8BIT &

   UART_1STOPBIT;

   U1STAvalue = UART_INT_TX_BUF_EMPTY &

   UART_IrDA_POL_INV_ZERO & UART_SYNC_BREAK_DISABLED &

   UART_TX_ENABLE & UART_INT_RX_3_4_FUL &

   UART_ADR_DETECT_DIS &

   UART_RX_OVERRUN_CLEAR;

   OpenUART1(U1MODEvalue, U1STAvalue, baudvalue);

/* Load transmit buffer and transmit the same till null character is

   encountered */

   putsUART1 ((unsigned int *)Txdata);

/* Wait for transmission to complete */

   while(BusyUART1());

/* Read all the data remaining in receive buffer which are unread */

   while(DataRdyUART1())

   {

       (*( Receiveddata)++) = ReadUART1() ;

   }

/* Turn off UART1 module */

   CloseUART1();

   return 0;

}

 

Example of Use ( AND_OR mask )

 

#define USE_AND_OR        /* To enable AND_OR mask setting */

#include<uart.h>

 

/* Received data is stored in array Buf */

char Buf[80];

char * Receivedddata = Buf;

 

/* This is UART1 transmit ISR */

void __attribute__((__interrupt__)) _U1TXInterrupt(void)

{

   IFS0bits.U1TXIF = 0;

}

 

/* This is UART1 receive ISR */

void __attribute__((__interrupt__)) _U1RXInterrupt(void)

{

   IFS0bits.U1RXIF = 0;

   /* Read the receive buffer until at least one or more characters

      can be read */

   while( DataRdyUART1())

   {

      ( *( Receiveddata)++) = ReadUART1();

   }

}

 

int main(void)

{

/* Data to be transmitted using UART communication module */

   char Txdata[] = {'M','i','c','r','o','c','h','i','p',

                    ' ','I','C','D','2','\0'};

/* Holds the value of baud register */

   unsigned int baudvalue;

/* Holds the value of uart config reg */

   unsigned int U1MODEvalue;

/* Holds the information regarding uart

   TX & RX interrupt modes */

   unsigned int U1STAvalue;

/* Turn off UART1module */

   CloseUART1();

/* Configure uart1 receive and transmit interrupt */

   ConfigIntUART1(UART_RX_INT_EN | UART_RX_INT_PR6 |

                  UART_TX_INT_DIS | UART_TX_INT_PR2);

/* Configure UART1 module to transmit 8 bit data with one stopbit.

   Also Enable loopback mode */

   baudvalue = 5;

   U1MODEvalue = UART_EN | UART_IDLE_CON |

   UART_DIS_WAKE | UART_EN_LOOPBACK |

   UART_EN_ABAUD | UART_NO_PAR_8BIT |

   UART_1STOPBIT;

   U1STAvalue = UART_INT_TX_BUF_EMPTY |

   UART_IrDA_POL_INV_ZERO | UART_SYNC_BREAK_DISABLED |

   UART_TX_ENABLE | UART_INT_RX_3_4_FUL |

   UART_ADR_DETECT_DIS |

   UART_RX_OVERRUN_CLEAR;

   OpenUART1(U1MODEvalue, U1STAvalue, baudvalue);

/* Load transmit buffer and transmit the same till null character is

   encountered */

   putsUART1 ((unsigned int *)Txdata);

/* Wait for transmission to complete */

   while(BusyUART1());

/* Read all the data remaining in receive buffer which are unread */

   while(DataRdyUART1())

   {

      (*( Receiveddata)++) = ReadUART1();

   }

/* Turn off UART1 module */

   CloseUART1();

   return 0;

}

 

3          Functions

3.1         BusyUART1 , BusyUART2

 

Function Prototype

char BusyUART1(void);

char BusyUART2(void);

Include

uart.h

Description

This function returns the UART transmission status

Arguments

None

Return Value

If ‘1’ is returned, it indicates that UART is busy in transmission and UxSTA<TRMT> bit is ‘0’.

If ‘0’ is returned, it indicates that UART is not busy and UxSTA<TRMT> bit is ‘1’.

Remarks:

This function returns the status of the UART. This indicates if the UART is busy in transmission as indicated by the UxSTA<TRMT> bit.

3.2         CloseUART1 , CloseUART2

 

Function Prototype

char BusyUART1(void);

char BusyUART2(void);

Include

uart.h

Description

This function returns the UART transmission status

Arguments

None

Return Value

None

Remarks:

This function first turns off the UART module and then disables the UART transmit and receive interrupts. The Interrupt Flag bits are also cleared.

3.3         ConfigIntUART1 , ConfigIntUART2

 

Function Prototype

void ConfigIntUART1(unsigned int config);

void ConfigIntUART2(unsigned int config);

Include

uart.h

Description

This function configures the UART Interrupts.

Arguments

config - Individual interrupt enable/disable information as defined below:

Receive Interrupt enable

   UART_RX_INT_EN

   UART_RX_INT_DIS

Receive Interrupt Priority

   UART_RX_INT_PR0

   UART_RX_INT_PR1

   UART_RX_INT_PR2

   UART_RX_INT_PR3

   UART_RX_INT_PR4

   UART_RX_INT_PR5

   UART_RX_INT_PR6

   UART_RX_INT_PR7

Transmit Interrupt enable

   UART_TX_INT_EN

   UART_TX_INT_DIS

Transmit Interrupt Priority

   UART_TX_INT_PR0

   UART_TX_INT_PR1

   UART_TX_INT_PR2

   UART_TX_INT_PR3

   UART_TX_INT_PR4

   UART_TX_INT_PR5

   UART_TX_INT_PR6

   UART_TX_INT_PR7

Return Value

None

Remarks:

This function enables/disables the UART transmit and receive interrupts and sets the interrupt priorities.

3.4         DataRdyUART1 , DataRdyUART2

 

Function Prototype

char DataRdyUART1(void);

char DataRdyUART2(void);

Include

uart.h

Description

This function returns the UART receive buffer status.

Arguments

None

Return Value

If ‘1’ is returned, it indicates that the receive buffer has a data to be read.

If ‘0’ is returned, it indicates that receive buffer does not have any new data to be read.

Remarks:

This function returns the status of the UART receive buffer. This indicates if the UART receive buffer contains any new data that is yet to be read as indicated by the UxSTA<URXDA> bit.

3.5         OpenUART1 , OpenUART2

 

Function Prototype

void OpenUART1(unsigned int config1,

   unsigned int config2, unsigned int ubrg);

void OpenUART2(unsigned int config1,

   unsigned int config2, unsigned int ubrg);

Include

uart.h

Description

This function configures the UART module

Arguments

config1 - The parameters to be configured in the UxMODE register as defined below:

UART enable/disable

   UART_EN

   UART_DIS

UART Idle mode operation

   UART_IDLE_CON

   UART_IDLE_STOP

UART communication with ALT pins*

   UART_ALTRX_ALTTX

   UART_RX_TX

* UART communication with ALT pins is available only for certain devices and the suitable data sheet should be referred to.

UART Wake-up on Start

   UART_EN_WAKE

   UART_DIS_WAKE

UART Loopback mode enable/disable

   UART_EN_LOOPBACK

   UART_DIS_LOOPBACK

Input to Capture module

   UART_EN_ABAUD

   UART_DIS_ABAUD

Parity and data bits select

   UART_NO_PAR_9BIT

   UART_ODD_PAR_8BIT

   UART_EVEN_PAR_8BIT

   UART_NO_PAR_8BIT

Number of Stop bits

   UART_2STOPBITS

   UART_1STOPBIT

 

config2 - The parameters to be configured in the UxSTA register as defined below:

UART Transmission mode interrupt select

   UART_INT_TX_BUF_EMPTY

   UART_INT_TX_LAST_CH 

   UART_INT_TX_EACH_CHAR

IrDA Polarity

   UART_IrDA_POL_INV_ONE

   UART_IrDA_POL_INV_ZERO

 

 

UART Transmit Break bit

   UART_TX_PIN_NORMAL

   UART_TX_PIN_LOW

UART transmit enable/disable

   UART_TX_ENABLE

   UART_TX_DISABLE

UART Receive Interrupt mode select

   UART_INT_RX_BUF_FUL

   UART_INT_RX_3_4_FUL

   UART_INT_RX_CHAR

UART address detect enable/disable

   UART_ADR_DETECT_EN

   UART_ADR_DETECT_DIS

UART OVERRUN bit clear

   UART_RX_OVERRUN_CLEAR

 

ubrg - The value to be written into UxBRG register to set the baud rate.

Return Value

None

Remarks:

This functions configures the UART transmit and receive sections and sets the communication baud rate

3.6         ReadUART1 , ReadUART2

 

Function Prototype

unsigned int ReadUART1(void);

unsigned int ReadUART2(void);

Include

uart.h

Description

This function returns the content of UART receive buffer (UxRXREG) register.

Arguments

None

Return Value

This function returns the contents of Receive buffer (UxRXREG) register.

Remarks:

This function returns the contents of the Receive Buffer register.

If 9 bit reception is enabled, the entire register content is returned.

If 8 bit reception is enabled, then register is read and the 9th bit is masked.

3.7         WriteUART1 , WriteUART2

 

Function Prototype

void WriteUART1(unsigned int data);

void WriteUART2(unsigned int data);

Include

uart.h

Description

This function writes data to be transmitted into the transmit buffer (UxTXREG) register.

Arguments

data - The data to be transmitted.

Return Value

None

Remarks:

This function writes the data to be transmitted into the transmit buffer.

If 9-bit transmission is enabled, the 9-bit value is written into the transmit buffer.

If 8-bit transmission is enabled, then upper byte is masked and then written into the transmit buffer.

3.8         getsUART1 , getsUART2

 

Function Prototype

unsigned int getsUART1(unsigned int length,

   unsigned int *buffer, unsigned int uart_data_wait);

unsigned int getsUART2(unsigned int length,

   unsigned int *buffer, unsigned int uart_data_wait);

Include

uart.h

Description

This function reads a string of data of specified length and stores it into the buffer location specified.

Arguments

length - The length of the string to be received.

buffer - The pointer to the location where the data received have to be stored.

uart_data_wait - The time-out count for which the module has to wait before return. If the time-out count is ‘N’, the actual time-out would be about (19 * N – 1) instruction cycles.

Return Value

This function returns the number of bytes yet to be received.

If the return value is ‘0’, it indicates that the complete string has been received.

If the return value is non-zero, it indicates that the complete string has not been received

Remarks:

None

3.9         putsUART1 , putsUART2

 

Function Prototype

void putsUART1(unsigned int *buffer);

void putsUART2(unsigned int *buffer);

Include

uart.h

Description

This function writes a string of data to be transmitted into the UART transmit buffer.

Arguments

buffer - The pointer to the string of data to be transmitted.

Return Value

None

Remarks:

This function writes the data to be transmitted into the transmit buffer until NULL character is encountered. Once the transmit buffer is full, it waits until data gets transmitted and then writes the next data into the Transmit register.

3.10     getcUART1 , getcUART2

 

Include

uart.h

Description

This function is identical to ReadUART1 and ReadUART2, i.e.,

#define to ReadUART1 and ReadUART2 in uart.h.

 

3.11     putcUART1 , putcUART2

 

Include

uart.h

Description

This function is identical to WriteUART1 and WriteUART2, i.e.,

#define to WriteUART1 and WriteUART2 in uart.h.

 

4          Macros

4.1         EnableIntU1RX , EnableIntU2RX

 

Macro

EnableIntU1RX

EnableIntU2RX

Include

uart.h

Description

This macro sets UART Receive Interrupt Enable bit of Interrupt Enable Control register

Arguments

None

Remarks

None

4.2         EnableIntU1TX , EnableIntU1TX

 

Macro

EnableIntU1TX

EnableIntU2TX

Include

uart.h

Description

This macro enables the UART transmit interrupt.

Arguments

None

Remarks

This macro sets UART Transmit Interrupt Enable bit of Interrupt Enable Control register.

4.3         DisableIntU1RX , DisableIntU1RX

 

Macro

DisableIntU1RX

DisableIntU2RX

Include

uart.h

Description

This macro disables the UART receive interrupt

Arguments

None

Remarks

This macro clears UART Receive Interrupt Enable bit of Interrupt Enable Control register.

4.4         DisableIntU1TX , DisableIntU1TX

 

Macro

DisableIntU1TX

DisableIntU2TX

Include

uart.h

Description

This macro disables the UART transmit interrupt.

Arguments

None

Remarks

This macro clears UART Transmit Interrupt Enable bit of Interrupt Enable Control register

4.5         SetPriorityIntU1RX SetPriorityIntU2RX

 

Macro

SetPriorityIntU1RX

SetPriorityIntU2RX

Include

uart.h

Description

This macro sets priority for UART receive interrupt.

Arguments

Priority

Remarks

This macro sets UART Receive Interrupt Priority bits of Interrupt Priority Control register.

4.6         SetPriorityIntU1TX , SetPriorityIntU2TX

 

Macro

SetPriorityIntU1TX

SetPriorityIntU2TX

Include

uart.h

Description

This macro sets priority for UART transmit interrupt

Arguments

priority

Remarks

This macro sets UART Transmit Interrupt Priority bits of Interrupt Priority Control register.